package redis

import (
	beego "github.com/beego/beego/v2/server/web"
	"os"
	"os/signal"
	"syscall"
)

var (
	Rd DataRedis
)

func init() {
	host, _ := beego.AppConfig.String("redis.host")
	pwd, _ := beego.AppConfig.String("redis.pwd")
	db, _ := beego.AppConfig.String("redis.db")

	if db == "" {
		db = "0"
	}

	// redis test 库初始化
	Rd = DataRedis{
		RedisHost: host,
		RedisPwd:  pwd,
		RedisDB:   db,
		Timeout:   20,
		RedisPool: nil,
	}
	Rd.RedisPool = Rd.NewPool()
	cleanupHook()
}

func cleanupHook() {
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	signal.Notify(c, syscall.SIGTERM)
	signal.Notify(c, syscall.SIGKILL)
	go func() {
		<-c
		err := Rd.RedisPool.Close()
		if err != nil {
			return
		}
		os.Exit(0)
	}()
}
